package com.hj.chartdemo.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.github.mikephil.charting.data.Entry
import com.hj.chartdemo.adapter.ChartPieAdapter
import com.hj.chartdemo.adapter.ChartPieTabAdapter
import com.hj.chartdemo.bean.chart.ChartItemData
import com.hj.chartdemo.bean.chart.ChartTabData
import com.hj.chartdemo.chart.PieChartManagger
import com.hj.chartdemo.databinding.ChatPieViewBinding
import com.hj.chartdemo.utils.SpaceItemDecoration
import java.util.ArrayList

/**
 * 饼图
 */
class ChatPieView : LinearLayout {
    var activity: AppCompatActivity? = null

    var binding: ChatPieViewBinding? = null

    var datas: ArrayList<ChartTabData>? = null

    var mContext: Context? = null

    init {
        activity = context as AppCompatActivity
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.mContext = context

        binding = ChatPieViewBinding.inflate(LayoutInflater.from(getContext()), this, true)
        initView()
    }
    private lateinit var tabAdapter: ChartPieTabAdapter
    private lateinit var adapter: ChartPieAdapter
    private fun initView() {
        PieChartManagger.getInstance().init(binding?.pieChart)

        binding?.pieTabRecycler?.setLayoutManager(GridLayoutManager(this.context, 4))
        binding?.pieTabRecycler?.addItemDecoration(SpaceItemDecoration(this.context, 8, 8, 4))
        tabAdapter = ChartPieTabAdapter(this.context)
        tabAdapter.setItemClickListener {
            clickTab(it)
        }
        binding?.pieTabRecycler?.adapter = tabAdapter
        binding?.pieTabRecycler?.setOverScrollMode(View.OVER_SCROLL_NEVER)

        binding?.pieRecycler?.setLayoutManager(GridLayoutManager(this.context, 3))
        binding?.pieRecycler?.addItemDecoration(SpaceItemDecoration(this.context, 8, 8, 3))
        adapter = ChartPieAdapter(this.context)
        adapter.setItemClickListener {
            clickOper(it)
        }
        binding?.pieRecycler?.adapter = adapter
        binding?.pieRecycler?.setOverScrollMode(View.OVER_SCROLL_NEVER)
    }

    fun setData(data: ArrayList<ChartTabData>) {
        data.get(0).selected = true
        this.datas = data
        if(data.size<4){
            binding?.pieTabRecycler?.setLayoutManager(GridLayoutManager(this.context, data.size))
        }
        tabAdapter.setData(datas)
        clickTab(data.get(0))
    }

    fun clickOper(chartItemData: ChartItemData){

    }

    fun clickTab(chartTabData: ChartTabData){
        val values = ArrayList<Entry>()
        var xlab = ArrayList<String>()
        var index = 1
        var d = chartTabData.data
        d.datas.forEach {
            values.add(Entry(index.toFloat(), it.value.toFloat(),it.label))
            xlab.add(it.label)
            index ++
        }
        PieChartManagger.getInstance().loadData(binding?.pieChart,values,ArrayList<String>(),d.colors,xlab,d.showLegend)
        adapter.setData(d.datas)
    }

}
